#include"main.h"
#include "GameAppImpl.h"
#include "../game/Game.h"

//globals
GameAppImpl*	g_pApp	= NULL;
Game*			g_pGame = NULL;

//void (*RenderFunc)() = NULL;
//void TestRender();

void Launch();
void Initialize();
void Release();

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreviousInstance,LPSTR lpcmdline,int nCmdShow)
{
	
	Launch();

	MSG Message;

	//window loop
	while(g_pApp->GetWindowStatus())
	{
		if(GetMessage(&Message,NULL,0,0))
		{
			if(!IsDialogMessage(g_pApp->GetWindowHandle(),&Message))
			{
				TranslateMessage(&Message);
				DispatchMessage(&Message);
			}
		}
	}

	//switch to D3D loop or quit
	if(!g_pApp->GetD3DStatus())
	{

		Release();
		return 0;
	}


	//RenderFunc = TestRender;

	//D3D loop
	
	Initialize();

	static float lastTime = (float)timeGetTime();
	while(g_pApp->GetD3DStatus())
	{
		if(PeekMessage(&Message,NULL,0,0,PM_REMOVE))
		{
			TranslateMessage(&Message);
			DispatchMessage(&Message);
		}

		if(g_pApp->CheckDevice())
		{
			float currTime = (float)timeGetTime();
			float timeDelta = (currTime - lastTime) * 0.001f;
			g_pApp->DoFrame( timeDelta );

			//if( RenderFunc != NULL )
			//{
			//	RenderFunc();
			//}
		}
	}

	Release();

	return 0;
}//WinMain


void Launch()
{
	g_pApp = new GameAppImpl();
}

void Initialize()
{
	g_pGame = GameInstance;

}

void Release()
{
	if( g_pApp != NULL )
	{
		delete g_pApp;
		g_pApp = NULL;
	}
}

//
//void TestRender()
//{
//	//	prepare data
//	LPDIRECT3DVERTEXBUFFER9 pTriangleVB = NULL;
//	LPDIRECT3DVERTEXBUFFER9 pQuadVB = NULL;
//	VOID* pData = NULL;
//	D3DVERTEX aTriangle[] = {	
//		{-2.0f,1.0f,10.0f,0xffff0000},
//		{-3.0f,-1.0f,10.0f,0xff00ff00},
//		{-1.0f,-1.0f,10.0f,0xff0000ff}
//	};
//	D3DVERTEX aQuad[] = {	{1.0f,-1.0f,10.0f,0xffffff00},
//							{3.0f,-1.0f,10.0f,0xffffff00},
//							{1.0f,1.0f,10.0f,0xffffff00},
//							{3.0f,1.0f,10.0f,0xffffff00}
//	};
//
//	//	create buffer to save data 
//	g_pApp->GetDevice()->CreateVertexBuffer(sizeof(aTriangle),D3DUSAGE_WRITEONLY,D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED,&pTriangleVB,NULL);
//	g_pApp->GetDevice()->CreateVertexBuffer(sizeof(aQuad),D3DUSAGE_WRITEONLY,D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED,&pQuadVB,NULL);
//	
//	pTriangleVB->Lock(0,0,(void**)&pData,0);
//	memcpy(pData,aTriangle,sizeof(aTriangle));
//	pTriangleVB->Unlock();
//
//	pQuadVB->Lock(0,0,(void**)&pData,0);
//	memcpy(pData,aQuad,sizeof(aQuad));
//	pQuadVB->Unlock();
//
//
//	//	set render state
//	/*D3DXMatrixPerspectiveFovLH(&m_matProjection,m_fFieldOfView,m_fAspectRatio,m_fNearPlane,m_fFarPlane);
//	m_pDirect3DDevice->SetTransform(D3DTS_PROJECTION,&m_matProjection);
//
//
//	m_pDirect3DDevice->SetRenderState(D3DRS_AMBIENT,RGB(255,255,255));
//	m_pDirect3DDevice->SetRenderState(D3DRS_LIGHTING,false);
//	m_pDirect3DDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
//	m_pDirect3DDevice->SetRenderState(D3DRS_ZENABLE,D3DZB_TRUE);
//
//	for(unsigned i = 0;i < 8;++i)
//	{
//		m_pDirect3DDevice->SetSamplerState(i,D3DSAMP_MINFILTER,D3DTEXF_LINEAR);
//		m_pDirect3DDevice->SetSamplerState(i,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR);
//		m_pDirect3DDevice->SetSamplerState(i,D3DSAMP_MIPFILTER,D3DTEXF_ANISOTROPIC);
//		m_pDirect3DDevice->SetSamplerState(i,D3DSAMP_MAXANISOTROPY,m_dwAnisotropy);
//	}
//
//	m_pDirect3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);*/
//
//
//	//	do render
//	g_pApp->GetDevice()->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);
//	g_pApp->GetDevice()->BeginScene();
//
//	g_pApp->GetDevice()->SetStreamSource(0,pTriangleVB,0,sizeof(D3DVERTEX));
//	g_pApp->GetDevice()->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
//
//	g_pApp->GetDevice()->SetStreamSource(0,pQuadVB,0,sizeof(D3DVERTEX));
//	g_pApp->GetDevice()->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);
//
//	g_pApp->GetDevice()->EndScene();
//	g_pApp->GetDevice()->Present(NULL,NULL,NULL,NULL);
//
//	//	release buffer
//	pTriangleVB->Release();
//	pQuadVB->Release();
//}